package com.douwong.student.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.contant.CommonConfig;
import com.douwong.student.fragment.MicroFragment;
import com.douwong.student.fragment.PrepareDetailFragment;
import com.douwong.student.fragment.PrepareFragment;
import com.douwong.student.fragment.PrepareLoadedFragment;
import com.douwong.student.fragment.PrepareLoadingFragment;
import com.douwong.student.fragment.PrepareResourceFragment;
import com.douwong.student.services.ResourceLoadService;
import com.douwong.student.view.CommSearch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @autor yichao
 * @time 2017/4/1114:59
 * @des 预习
 */
public class PrepareActivity extends BaseActivity {


    private ImageView go_back_img;
    public List<Map<String, Object>> mNavData;
    public Map<String, Object> mNavDataSelect;
    private LinearLayout prepare_ll, resource_ll, class_uplod_ll,
            class_loaded_ll, loading_hint_ll, class_video_ll;
    private ImageView prepare_img, resource_img, class_uplod_img,
            class_loaded_img, time_search, class_video_img;
    private TextView class_share_text, class_cloud_text, class_uplod_text,
            class_loaded_text, load_count_text, current_subject, select_time, class_video_text;
    private int loadingCount;
    public int selectItem = 0;
    private LinearLayout prepare_all_ll, delete_time_ll;
    public boolean hasChild = false;
    private CommSearch commSearch;
    private final static String TAG = "PrepareClassActivity";
    BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context paramContext, Intent paramIntent) {
            String action = paramIntent.getAction();
            if (action.equals(CommonConfig.RESOURCE_LOAD_ADD)) {
                loadingCount++;
                loading_hint_ll.setVisibility(View.VISIBLE);
                load_count_text.setText(loadingCount + "");
            } else if (action.equals(CommonConfig.RESOURCE_LOAD_REDUCE)) {
                loadingCount--;
                if (loadingCount == 0) {
                    loadingCount = 0;
                    loading_hint_ll.setVisibility(View.GONE);
                } else {
                    load_count_text.setText(loadingCount + "");
                }
            } else if (action.equals(CommonConfig.RESOURCE_LOAD_COUNT)) {
                loadingCount = paramIntent.getIntExtra("loadCount", 0);
                if (loadingCount > 0) {
                    loading_hint_ll.setVisibility(View.VISIBLE);
                    load_count_text.setText(loadingCount + "");
                }
            }
        }
    };
    IntentFilter filter = new IntentFilter() {
        {
            addAction(CommonConfig.RESOURCE_LOAD_ADD);
            addAction(CommonConfig.RESOURCE_LOAD_REDUCE);
            addAction(CommonConfig.RESOURCE_LOAD_COUNT);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prepare);
        registerReceiver(receiver, filter);
        initViews();
        commSearch = new CommSearch(this);
        initListView();
    /*    if(MyApp.storageMounted()){
            MyApp.createAppDirs();
            MainActivity.createUserDirs();
        }*/
        Intent intent = new Intent();
        intent.setClass(PrepareActivity.this, ResourceLoadService.class);
        startService(intent);
    }

    private void initViews() {
        go_back_img = (ImageView) findViewById(R.id.go_back_img);
        prepare_img = (ImageView) findViewById(R.id.class_share_img);
        resource_img = (ImageView) findViewById(R.id.class_cloud_img);
        class_uplod_img = (ImageView) findViewById(R.id.class_uplod_img);
        class_loaded_img = (ImageView) findViewById(R.id.class_loaded_img);
        time_search = (ImageView) findViewById(R.id.time_search);
        resource_img.setImageResource(R.mipmap.prepare_resource_1);
        time_search.setVisibility(View.GONE);
        current_subject = (TextView) findViewById(R.id.classroom_chooseSubject);
        class_share_text = (TextView) findViewById(R.id.class_share_text);
        class_cloud_text = (TextView) findViewById(R.id.class_cloud_text);
        class_uplod_text = (TextView) findViewById(R.id.class_uplod_text);
        class_loaded_text = (TextView) findViewById(R.id.class_loaded_text);
        load_count_text = (TextView) findViewById(R.id.load_count_text);
        select_time = (TextView) findViewById(R.id.select_time);
        class_share_text.setText(getResources().getString(
                R.string.prepare_class));
        class_cloud_text.setText(getResources().getString(
                R.string.prepare_resource));
        class_video_ll = (LinearLayout) findViewById(R.id.class_video_ll);
        class_video_ll.setVisibility(View.VISIBLE);
        class_video_img = (ImageView) findViewById(R.id.class_video_img);
        class_video_text = (TextView) findViewById(R.id.class_video_text);
        delete_time_ll = (LinearLayout) findViewById(R.id.delete_time_ll);
        prepare_ll = (LinearLayout) findViewById(R.id.class_share_ll);
        resource_ll = (LinearLayout) findViewById(R.id.class_cloud_ll);
        class_uplod_ll = (LinearLayout) findViewById(R.id.class_uplod_ll);
        class_loaded_ll = (LinearLayout) findViewById(R.id.class_loaded_ll);
        loading_hint_ll = (LinearLayout) findViewById(R.id.loading_hint_ll);
        prepare_all_ll = (LinearLayout) findViewById(R.id.prepare_all_ll);

        ClassOnFocusListener onFoucsClik = new ClassOnFocusListener();
        PrepareOnClick clickListener = new PrepareOnClick();

        class_video_ll.setOnFocusChangeListener(onFoucsClik);
        prepare_ll.setOnFocusChangeListener(onFoucsClik);
        resource_ll.setOnFocusChangeListener(onFoucsClik);
        class_uplod_ll.setOnFocusChangeListener(onFoucsClik);
        class_loaded_ll.setOnFocusChangeListener(onFoucsClik);
        go_back_img.setOnClickListener(clickListener);
        prepare_all_ll.setOnClickListener(clickListener);
        time_search.setOnClickListener(clickListener);
        delete_time_ll.setOnClickListener(clickListener);

    }

    // 选项切换判断

    private class ClassOnFocusListener implements View.OnFocusChangeListener {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            switch (v.getId()) {

                // 备课
                case R.id.class_share_ll:
                    if (hasFocus) {
                        prepare_img.setImageResource(R.mipmap.prepare_2);
                        prepare_ll.setBackgroundColor(getResources().getColor(
                                R.color.class_file_word));
                        class_share_text.setTextColor(0Xff7d7d7d);
                        if (hasChild) {
                            selectItem = 4;
                            showFragment(mNavData.get(4));
                        } else {
                            selectItem = 0;
                            showFragment(mNavData.get(0));
                        }
                    } else {
                        prepare_img.setImageResource(R.mipmap.prepare);
                        prepare_ll.setBackgroundColor(getResources().getColor(
                                R.color.class_file_leftbackground));
                        class_share_text.setTextColor(0Xffffffff);
                    }
                    break;

                // 资源
                case R.id.class_cloud_ll:
                    selectItem = 1;
                    if (hasFocus) {
                        resource_img
                                .setImageResource(R.mipmap.prepare_resource_2);
                        resource_ll.setBackgroundColor(getResources().getColor(
                                R.color.class_file_word));
                        class_cloud_text.setTextColor(0Xff7d7d7d);
                        showFragment(mNavData.get(1));
                    } else {
                        resource_img
                                .setImageResource(R.mipmap.prepare_resource_1);
                        resource_ll.setBackgroundColor(getResources().getColor(
                                R.color.class_file_leftbackground));
                        class_cloud_text.setTextColor(0Xffffffff);
                    }
                    break;
                // 正在下载
                case R.id.class_uplod_ll:
                    selectItem = 2;
                    if (hasFocus) {
                        class_uplod_img.setImageResource(R.mipmap.class_upload_2);
                        class_uplod_ll.setBackgroundColor(getResources().getColor(
                                R.color.class_file_word));
                        class_uplod_text.setTextColor(0Xff7d7d7d);
                        showFragment(mNavData.get(2));
                    } else {
                        class_uplod_img.setImageResource(R.mipmap.class_uplod1);
                        class_uplod_ll.setBackgroundColor(getResources().getColor(
                                R.color.class_file_leftbackground));
                        class_uplod_text.setTextColor(0Xffffffff);
                    }
                    break;
                // 已下载
                case R.id.class_loaded_ll:
                    selectItem = 3;
                    if (hasFocus) {
                        class_loaded_img.setImageResource(R.mipmap.class_loaded2);
                        class_loaded_ll.setBackgroundColor(getResources().getColor(
                                R.color.class_file_word));
                        class_loaded_text.setTextColor(0Xff7d7d7d);
                        showFragment(mNavData.get(3));
                    } else {
                        class_loaded_img.setImageResource(R.mipmap.class_loaded1);
                        class_loaded_ll.setBackgroundColor(getResources().getColor(
                                R.color.class_file_leftbackground));
                        class_loaded_text.setTextColor(0Xffffffff);
                    }
                    break;
                //微课
                case R.id.class_video_ll:
                    selectItem = 5;
                    if (hasFocus) {
                        class_video_img.setImageResource(R.mipmap.choosevideo);
                        class_video_ll.setBackgroundColor(getResources().getColor(R.color.class_file_word));
                        class_video_text.setTextColor(0Xff7d7d7d);
                        showFragment(mNavData.get(5));
                    } else {
                        class_video_img.setImageResource(R.mipmap.video);
                        class_video_ll.setBackgroundColor(getResources().getColor(R.color.class_file_leftbackground));
                        class_video_text.setTextColor(0Xffffffff);
                    }
                    break;

            }
            if (selectItem == 0) {
                prepare_all_ll.setVisibility(View.VISIBLE);
                //				time_search.setVisibility(View.VISIBLE);
            } else {
                prepare_all_ll.setVisibility(View.GONE);
                delete_time_ll.setVisibility(View.GONE);
                //				time_search.setVisibility(View.GONE);
            }
        }
    }

    private class PrepareOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                // 返回
                case R.id.go_back_img:
                    Log.e("selectItem", selectItem + "");
                    if (selectItem == 4) {
                        selectItem = 0;
                        hasChild = false;
                        showFragment(mNavData.get(0));
                    } else {
                        finish();
                    }
                    break;
                //全部
                case R.id.prepare_all_ll:
                    commSearch.showSubject(current_subject,
                            delete_time_ll, CommonConfig.PREPARE_SEARCH);
                    break;
                //时间搜索
                case R.id.time_search:
                    commSearch.showTimeWindow(
                            delete_time_ll, select_time, CommonConfig.PREPARE_SEARCH);
                    break;
                //取消时间搜索
                case R.id.delete_time_ll:
                    delete_time_ll.setVisibility(View.GONE);
                    commSearch.setStartTime(null,CommonConfig.PREPARE_SEARCH);
                    break;
                default:
                    break;
            }
        }

    }

    /**
     * 显示内容
     *
     * @param map
     */
    public void showFragment(Map<String, Object> map) {
        mNavDataSelect = map;
        FragmentManager fragmentManager = getFragmentManager();
        // mainNavAdapter.notifyDataSetChanged();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        Fragment fragment = (Fragment) map.get("fragment");
        boolean isInit = Boolean.parseBoolean(map.get("isInit").toString());

        // 检查是否存在
        if (!isInit) {
            transaction.add(R.id.frament, fragment);
            map.put("isInit", true);
            Log.d(TAG, "isInit is false........");
        }

        // 隐藏&显示控件
        for (Map<String, Object> item : mNavData) {
            Fragment itemFragment = (Fragment) item.get("fragment");
            if (itemFragment == null) {
                continue;
            }
            if (itemFragment.equals(fragment)) {
                //				Log.d(TAG,"show fragm : "+fragment);
                transaction.show(itemFragment);
                if (itemFragment instanceof PrepareLoadedFragment) {
                    ((PrepareLoadedFragment) itemFragment).updateDisplay();
                }
            } else {
                transaction.hide(itemFragment);
            }
        }
        transaction.commit();
    }

    private void initListView() {

        mNavData = new ArrayList<Map<String, Object>>();
        Map<String, Object> map ;
        //预习
        map = new HashMap<String, Object>();
        map.put("fragment", new PrepareFragment(PrepareActivity.this));
        map.put("isInit", true);
        mNavData.add(map);
        //资源
        map = new HashMap<String, Object>();
        map.put("fragment", new PrepareResourceFragment(1));
        map.put("isInit", true);
        mNavData.add(map);
        //正在下载
        map = new HashMap<String, Object>();
        map.put("fragment", new PrepareLoadingFragment());
        map.put("isInit", true);
        mNavData.add(map);
        //已下载
        map = new HashMap<String, Object>();
        map.put("fragment", new PrepareLoadedFragment(3));
        map.put("isInit", true);
        mNavData.add(map);

        map = new HashMap<String, Object>();
        map.put("fragment",
                new PrepareDetailFragment());
        map.put("isInit", true);
        mNavData.add(map);
        //微课
        map = new HashMap<String, Object>();
        map.put("fragment",
                new MicroFragment());
        map.put("isInit", true);
        mNavData.add(map);

        // map = new HashMap<String, Object>();
        // map.put("fragment", new LoadAllFragment(PrepareClassActivity.this));
        // map.put("isInit", true);
        // mNavData.add(map);

        this.initFragment();
    }
    //
    //
    // /**
    // * 初始化Fragment
    // */

    private void initFragment() {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        for (Map<String, Object> item : mNavData) {
            Fragment itemFragment = (Fragment) item.get("fragment");
            boolean isInit = Boolean
                    .parseBoolean(item.get("isInit").toString());
            if (isInit && itemFragment != null) {
                transaction.add(R.id.frament, itemFragment);
                transaction.hide(itemFragment);
            }
        }

        transaction.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            // 返回按钮点击时，做相应处理
            Log.e("selectItem", "" + selectItem);
            if (selectItem == 4) {
                showFragment(mNavData.get(0));
                selectItem = 0;
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    protected void initView() {
    }
    public static  void  startActivity(Context context){
    Intent intent=new Intent(context,PrepareActivity.class);
    context.startActivity(intent);
}
}
